package com.ruoyi.asset.roadgreening.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.asset.roadgreening.domain.bo.MaintenanceScopeSearchBo;
import com.ruoyi.asset.roadgreening.domain.bo.RoadGreeningOverviewBo;
import com.ruoyi.asset.roadgreening.domain.bo.StatisticianGreeneryByRegion;
import com.ruoyi.asset.roadgreening.domain.vo.GreeneryCommunityCountVo;
import com.ruoyi.asset.roadgreening.domain.vo.GreeneryCommunityMapVo;
import com.ruoyi.asset.roadgreening.domain.vo.GreeneryWorkStatisticsVo;
import com.ruoyi.asset.roadgreening.domain.vo.RoadGreeningVo;
import com.ruoyi.asset.roadgreening.service.IRoadGreeningService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 道路绿化管养任务完成进度
 *
 * @author cc
 * @date 2023-08-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/asset/greenery")
public class RoadGreeningController extends BaseController {

	private final IRoadGreeningService roadGreeningService;

	/**
	 * 获取道路绿化管养-管理范围导入模板
	 */
	@SaIgnore()
	@Log(title = "道路绿化导入", businessType = BusinessType.IMPORT)
	@RepeatSubmit()
	@PostMapping(value = "/importData")
	public R<List<String>> importData(@RequestParam(name = "excelUrl") String excelUrl) {
		List<String> list = this.roadGreeningService.importExcel(excelUrl);

		if (list == null) {
			return R.fail();
		} else {
			return R.ok(list);
		}
	}

	/**
	 * 获取道路绿化管养-管理范围导入模板
	 */
	@SaIgnore()
	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response) {
		ExcelUtil.exportExcel(new ArrayList<>(), "道路绿化管养管理范围导入模板", RoadGreeningVo.class, response);
	}

	/**
	 * 删除道路绿化管养范围
	 */
	@SaIgnore()
	@DeleteMapping("/deleteMaintenanceScope/{ids}")
	public R<Void> deleteMaintenanceScope(@NotEmpty(message = "主键不能为空")
               @PathVariable Set<Long> ids) {
		int count = this.roadGreeningService.deleteMaintenanceScope(ids);

		return toAjax(count >= 0 ? 1 : count);
	}

	/**
	 * 查询道路绿化管养
	 */
	@SaIgnore()
	@GetMapping("/queryMaintenanceScope")
	public TableDataInfo<RoadGreeningVo> queryMaintenanceScope(MaintenanceScopeSearchBo searchBo, PageQuery pageQuery) {
        return this.roadGreeningService.queryMaintenanceScope(searchBo, pageQuery);
	}

	/**
	 * 导出道路绿化管养
	 */
	@SaIgnore()
    @Log(title = "道路绿化", businessType = BusinessType.EXPORT)
	@PostMapping("/exportMaintenanceScope")
	public void exportMaintenanceScope(MaintenanceScopeSearchBo searchBo, HttpServletResponse response) {
		List<RoadGreeningVo> list = this.roadGreeningService.queryMaintenanceScope(searchBo);

		ExcelUtil.exportExcel(list, "道路绿化管养管理范围", RoadGreeningVo.class, response);
	}

	/**
	 * 按区统计绿地
	 */
	@SaIgnore()
	@GetMapping("/statForGreeneryByRegion")
	public R<List<StatisticianGreeneryByRegion>> statForGreeneryByRegion() {
		List<StatisticianGreeneryByRegion> stat = this.roadGreeningService.statForGreeneryByRegion();

		return R.ok(stat);
	}

    /**
     * 按区统计绿地
     */
    @SaIgnore()
    @GetMapping("/greeneryCount")
    public R<RoadGreeningOverviewBo> greeneryCount() {
        RoadGreeningOverviewBo roadGreeningOverview = this.roadGreeningService.getRoadGreeningOverview();
        return R.ok(roadGreeningOverview);
    }

    /**
     * 按区统统计绿地
     */
    @SaIgnore()
    @GetMapping("/roadGreeningCommunityCount")
    public R<Map> roadGreeningCommunityCount() {
        Map map = this.roadGreeningService.getRoadGreeningCommunityCount();
        return R.ok(map);
    }

    /**
     * 查询智慧大屏道路绿化玉塘街道全社区
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/greeneryCommunity")
    public R<List<GreeneryCommunityCountVo>> greeneryCommunity(String date) {
        date = "2024-03-13";
        List<GreeneryCommunityCountVo> greeneryCommunityCountVos = roadGreeningService.getCleaningCountByCommunity(date);
        return R.ok(greeneryCommunityCountVos);
    }

    /**
     * 查询智慧大屏道路绿化今日作业统计与百分比
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/greeneryWorkByDate")
    public R<GreeneryWorkStatisticsVo> greeneryWorkByDate(String date) {
        date = "2024-03-13";
        GreeneryWorkStatisticsVo greeneryWorkByDate = roadGreeningService.getGreeneryWorkByDate(date);
        return R.ok(greeneryWorkByDate);
    }

    /**
     * 查询智慧大屏道路绿化今日作业统计与百分比
     * @param date 日期
     * @param community 社区
     * @return
     */
    @SaIgnore()
    @GetMapping("/greeneryWorkByDateAndCommunity")
    public R<GreeneryWorkStatisticsVo> greeneryWorkByDateAndCommunity(String date,String community) {
        date = "2024-03-13";
        GreeneryWorkStatisticsVo greeneryWorkByDate = roadGreeningService.getGreeneryWorkByDateAndCommunity(date,community);
        return R.ok(greeneryWorkByDate);
    }

    /**
     * 查询智慧大屏道路绿化按社区区分
     * @param community
     * @param date
     * @return
     */
    @SaIgnore()
    @GetMapping("/communityByDate")
    public R<GreeneryCommunityMapVo> communityByDate(String community, String date) {
        date = "2024-03-13";
        GreeneryCommunityMapVo communityByDate = roadGreeningService.getCommunityByDate(community, date);
        return R.ok(communityByDate);
    }
}
